package com.create.rdjc.jdbc.source.impl;

import cn.hutool.core.map.MapBuilder;
import cn.hutool.core.map.MapUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.Page;
import cn.hutool.db.PageResult;
import cn.hutool.db.ds.DSFactory;
import com.create.rdjc.config.Config;
import com.create.rdjc.jdbc.SqlModel;
import com.create.rdjc.jdbc.source.SourceJdbc;
import com.create.rdjc.model.TableFiledModel;
import com.create.rdjc.model.TableModel;
import com.create.rdjc.util.TableUtils;

import java.sql.SQLException;
import java.util.List;

public class MysqlJdbc implements SourceJdbc {
    private  Db db = Db.use(Config.mysql);
    private  SqlModel sqlModel = new SqlModel(Config.mysqlProperties);


    /**
     * 查询所有表字段
     * @return
     */
    public List<TableModel> selectFields() {

        try {
            String catalog = db.getConnection().getCatalog();
            //MapBuilder<String, String> schema = MapUtil.builder("schema", catalog);
            List<TableFiledModel> query = db.query(sqlModel.getSql("selectFields"), TableFiledModel.class, catalog,catalog);
            List<TableModel> tableList = TableUtils.getTableList(query);
            return tableList;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 分页查询所有数据
     * @param tableName
     * @param pageNumber
     * @param pageSize
     * @return
     */
    public PageResult<Entity> findAll(String tableName,int pageNumber, int pageSize){
        try {
           return db.page(new Entity(tableName), new Page(pageNumber, pageSize));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return new PageResult<>();
    }

    public void clase(){

    }

}
